<template>
  <a-menu
    v-model:selectedKeys="selectedKeys"
    v-model:openKeys="openKeys"
    mode="horizontal"
    class="nav-menu"
  >
    <a-menu-item key="home">
      <router-link to="/">
        <home-outlined />
        <span>首页</span>
      </router-link>
    </a-menu-item>

    <a-sub-menu key="categories">
      <template #title>
        <appstore-outlined />
        <span>分类</span>
      </template>
      <a-menu-item key="tech">
        <router-link to="/tech">技术交流</router-link>
      </a-menu-item>
      <a-menu-item key="life">
        <router-link to="/life">生活分享</router-link>
      </a-menu-item>
      <a-menu-item key="chat">
        <router-link to="/chat">闲聊灌水</router-link>
      </a-menu-item>
    </a-sub-menu>

    <template v-if="isLoggedIn">
      <a-menu-item key="create-post">
        <router-link to="/create-post">
          <edit-outlined />
          <span>发帖</span>
        </router-link>
      </a-menu-item>

      <a-menu-item key="messages">
        <router-link to="/messages">
          <message-outlined />
          <span>消息</span>
          <a-badge
            v-if="unreadCount > 0"
            :count="unreadCount"
            :offset="[10, -5]"
          />
        </router-link>
      </a-menu-item>

      <a-sub-menu key="user" class="float-right">
        <template #title>
          <div class="user-menu">
            <a-avatar :src="userAvatar" />
            <span>{{ username }}</span>
          </div>
        </template>
        <a-menu-item key="profile">
          <router-link to="/profile">
            <user-outlined />
            <span>个人主页</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="my-posts">
          <router-link to="/my-posts">
            <file-text-outlined />
            <span>我的帖子</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="favorites">
          <router-link to="/favorites">
            <star-outlined />
            <span>我的收藏</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="settings">
          <router-link to="/profile">
            <setting-outlined />
            <span>个人设置</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="logout" @click="handleLogout">
          <logout-outlined />
          <span>退出登录</span>
        </a-menu-item>
      </a-sub-menu>
    </template>

    <template v-else>
      <div class="float-right">
        <a-menu-item key="login">
          <router-link to="/login">登录</router-link>
        </a-menu-item>
        <a-menu-item key="register">
          <router-link to="/register">注册</router-link>
        </a-menu-item>
      </div>
    </template>
  </a-menu>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import {
  HomeOutlined,
  AppstoreOutlined,
  EditOutlined,
  MessageOutlined,
  UserOutlined,
  FileTextOutlined,
  StarOutlined,
  SettingOutlined,
  LogoutOutlined
} from '@ant-design/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const selectedKeys = ref([route.name])
const openKeys = ref(['categories'])
const unreadCount = ref(0)

// 计算属性
const isLoggedIn = computed(() => userStore.isLoggedIn)
const username = computed(() => userStore.user?.username || '')
const userAvatar = computed(() => userStore.user?.avatar || '')

// 退出登录
const handleLogout = async () => {
  await userStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.nav-menu {
  padding: 0 20px;
}

.float-right {
  float: right;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 8px;
}

:deep(.ant-menu-item) {
  a {
    color: inherit;
    text-decoration: none;
  }
}
</style>
